import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Visitor from '../views/Visitor.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/Visitor',
    component: Visitor
  },
  {
    path: '/Login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (tokenStr !== 'ok') return next('/login')
  next()
})

export default router
